import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { BusinessMInfo, EquipMWarnHandle } from "@/api/backend/index";
import { useAccess } from "../DeviceM/access";
import qs from "qs";
import { mapCompanies } from "@/utils/common";

interface BusinessRows {
  addr: string | null;
  code: string;
  createBy: string | null;
  createTime: string | null;
  creditCode: string | null;
  id: number;
  isDel: number;
  name: string;
  params: anyObj;
  region: number | null;
  regionName: string | null;
  remark: string | null;
  searchValue: string | null;
  state: string | null;
  tel: string | null;
  type: string | null;
  updateBy: string | null;
  updateTime: string | null;
  user: string | null;
}
interface BusinessRes {
  total: number;
  msg: string;
  rows: BusinessRows[];
  code: number;
}

export const useBusiness = defineStore(Names.business, {
  state: () => {
    return {
      loading: false,
      query: "", //设备接入企业的id
      item: {
        id: 3166,
        name: "江苏融知达信息科技有限公司", //单位名称
        department_id: 3,
        creditCode: "91320402MA1XNB5X6C", //企业社会信用代码
        user: "周伟军", //联系人
        tel: "13092582600", //联系方式
        contact2: "",
        regionName: "",
        tel2: "",
        addr: "", //企业地址
        bank: "", //指定银行
        bank_manager: "", //银行客户经理
        bank_tel: "",
        city_id: 1,
        city_name: "常州市",
        area_id: 4,
        region: "新北区",
        industry_id: 9,
        type: "其他制造业", //所属行业
        hours: 4,
        status: 1,
        createTime: "2022-11-02 08:23:06", //创建时间
        update_time: "2022-11-09 17:10:09", //更新时间
        is_delete: 0,
        ip: "127.0.0.1",
        lat: "",
        log: "",
        status_text: "正常", //状态
      }, //单个企业详情
      devicesItem: {}, //企业内单个设备详情
      companyList: [], //企业列表数据
      companiesMap: new Map(), //企业列表map格式
    };
  },
  getters: {},
  actions: {
    //详情跳转需要获取设备列表
    getDeviceList() {
      const access = useAccess();
      return new Promise((resolve, reject) => {
        access
          .getDevicesListByOther({ institution: this.item.id })
          .then((res) => {
            resolve(res);
          });
      });
    },
    //获取企业列表数据 默认不用给regions数据但必须有该字段
    getCompanyList() {
      console.log("获取企业列表数据触发了");
      return new Promise((resolve, reject) => {
        _get(
          BusinessMInfo + "/list",
          { pageNum: 1, pageSize: 1000, regions: "" },
          "",
          ""
        )
          .then(async (res: any) => {
            this.companyList = res.rows; //将数据保存在state中
            this.companiesMap = await mapCompanies(res); //将数据变成Map数据格式
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //为大屏获取企业总数
    getCompanyListNumber(regions: any) {
      return new Promise((resolve, reject) => {
        _get(
          BusinessMInfo + "/list",
          { pageNum: 1, pageSize: 1000, regions: regions + "" },
          "",
          ""
        )
          .then(async (res: any) => {
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //重写一个获取axios
    roleCompany(data: any) {
      console.log("data", data);
      return new Promise((resolve, reject) => {
        _get(
          BusinessMInfo +
            "/list?" +
            qs.stringify(data, { arrayFormat: "repeat" }),
          {},
          "",
          ""
        )
          .then(async (res: any) => {
            this.companyList = res.rows; //将数据保存在state中
            this.companiesMap = await new Map(
              res.rows.map((item: any) => [item.id, item.name])
            ); //将数据变成Map数据格式
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //企业接入设备列表 维修记录查询
    getHandleInfo(data: any) {
      return new Promise((resolve, reject) => {
        _get(EquipMWarnHandle + "/list", data, "", "").then((res) => {
          console.log(res);
          resolve(res);
        });
      });
    },
    //单个企业数据
    getItemInfo(cname: string) {
      return new Promise((resolve, reject) => {
        _get(
          BusinessMInfo + "/list",
          { name: cname, regions: "" },
          "",
          ""
        ).then((res) => {
          console.log(res);
          resolve(res);
        });
      });
    },
  },
});
